package com.desksoft.ecommerce.util.mail;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.ServletContext;

import com.desksoft.ecommerce.util.holder.ApplicationContextHolder;
import com.desksoft.ecommerce.util.mail.template.TemplateBag;
import com.desksoft.utility.mail.message.JmsMailMessage;
import com.desksoft.utility.mail.message.MailMessage;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利 
 * 创建日期: 
 * 创建作者：luoy 
 * 文件名称： 邮件信件创建者
 * 版本： 
 * 功能： 
 * 最后修改时间：
 * 修改记录：
 *****************************************/
public class MailMessageCreator {

	protected Configuration stringConfiguration;
	protected Configuration fileConfiguration;
	
	/**
	 * 创建MailService可识别的信件
	 * @throws MessagingException 
	 */
	public static MailMessage createMailMessage(JmsMailMessage jmsMail) throws MessagingException{
		MailMessage mail = new MailMessage(jmsMail.getHtmlEncoding());
		mail.setFrom(jmsMail.getFrom());
		mail.setSendDate(jmsMail.getSendDate());
		mail.setTo(jmsMail.getTo());
		mail.setSubject(jmsMail.getSubject());
		mail.appendMailPart(mail.createHtmlPart(jmsMail.getBody()));
		return mail;
	}
	
	/**
	 * 创建Freemarker邮件正文
	 * @param	path	freemarker文件路径
	 * @param	params	参数
	 * @throws IOException 
	 * @throws TemplateException 
	 */
	public static String createBodyWithFreemarker(String path,String encoding,Map params) throws IOException, TemplateException{
		Template template = TemplateBag.getInstance().getTemplate(path);
		StringWriter writer = new StringWriter(); 
		template.process(params, writer);  
		return writer.toString();
	}
	
	
	/**
	 * 创建Jms传输邮件
	 * @param from	发信人地址
	 * @param to	收信人地址
	 * @param subject	信件标题
	 * @param sendDate	发送时间
	 * @return
	 */
	public static JmsMailMessage createJmsMessage(String from,String to,String subject,Date sendDate){
		JmsMailMessage mail = new JmsMailMessage();
		mail.setFrom(from);
		mail.setTo(to);
		mail.setSubject(subject);
		mail.setSendDate(sendDate);
		return mail;
	}
	
	/**
	 * 创建Jms传输邮件，默认使用管理员账户发送，默认现在就发送
	 */
	public static JmsMailMessage createJmsMessage(String to,String subject){
		JmsMailMessage mail = new JmsMailMessage();
		mail.setTo(to);
		mail.setSubject(subject);
		mail.setSendDate(new Date());
		return mail;
	}
	
	/**
	 * 加载附件
	 * @param mail
	 * @param path
	 */
	protected static void addAccessory(MailMessage mail,String path){
		// TODO
	}

	public Configuration getStringConfiguration() {
		return stringConfiguration;
	}

	public void setStringConfiguration(Configuration stringConfiguration) {
		this.stringConfiguration = stringConfiguration;
	}

	public Configuration getFileConfiguration() {
		return fileConfiguration;
	}

	public void setFileConfiguration(Configuration fileConfiguration) {
		this.fileConfiguration = fileConfiguration;
	}
	
}
